//
//  PopoverPresentationController.swift
//  DSWeibo
//
//  Created by liuzhu on 2018/11/10.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {
    var presentedFrame = CGRect.zero
    /// 用户创建负责转场动画的对象
    ///
    /// - Parameters:
    ///   - presentedViewController: 被展现的控制器，弹出来的那个
    ///   - presentingViewController: 发起的控制器
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
        
    }
    
    override func containerViewWillLayoutSubviews() {
        //修改弹出视图的尺寸（containerView:容器视图，presentedView：被展现的视图）
        if presentedFrame == CGRect.zero {
            presentedView?.frame = CGRect(x: 10, y: 90, width: UIScreen.main.bounds.size.width-20, height: UIScreen.main.bounds.size.height-180)
        }else{
            presentedView?.frame = presentedFrame
        }
        containerView?.insertSubview(coverView, at: 0)
    }
    
    private lazy var coverView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        view.frame = UIScreen.main.bounds
        let tap = UITapGestureRecognizer(target: self, action: #selector(close))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    @objc private func close(){
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}


